package cn.gson.springboot.controller.salecontroller;

import cn.gson.springboot.aop.MyLog;
import cn.gson.springboot.model.dao.saledao.OrderInfoDao;
import cn.gson.springboot.model.pjos.*;
import cn.gson.springboot.model.service.saleservice.ContractService;
import cn.gson.springboot.model.service.saleservice.OrderService;
import cn.gson.springboot.model.vo.aftervo.MainProductVo;
import cn.gson.springboot.model.vo.aftervo.MaintainChartVo;
import cn.gson.springboot.model.vo.salesvo.DeliveryplanVo;
import cn.gson.springboot.model.vo.salesvo.OrderProductNumberVo;
import cn.gson.springboot.model.vo.salesvo.SaExchangInfoVo;
import cn.gson.springboot.model.vo.salesvo.SaRetouerInfoVo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;

@Controller
public class OrderController {
    @Autowired
    ContractService contractService;
    @Autowired
    OrderService orderService;


    @RequestMapping("/t_pl")
    @ResponseBody
    public void xinzhuanp(Integer excId,Integer orderId,Integer deliverId,String exchangName,String ecName){
        orderService.toDayDeliveryInfo(excId,orderId,deliverId,exchangName, ecName);
    }

    @RequestMapping("/zhao_mainProduct")
    @ResponseBody
    public List<MainProductVo> jouerFhuoInfo(Integer deliverId){
        List<MainProductVo> mainProductVos = orderService.jouerFhuoInfo(deliverId);
        return mainProductVos;
    }

    @RequestMapping("/zhao_ZfhuodanEntity")
    @ResponseBody
    public List<ZfhuodanEntity> zfhuodanzhao(String selctInfo){
        List<ZfhuodanEntity> zfhuodanzhao = orderService.zfhuodanzhao(selctInfo);
        return zfhuodanzhao;
    };
    @RequestMapping("/zhao_saretouer")
    @ResponseBody
    public List<SaRetouerInfoVo> saRouterInfozhao(String searchInfo){
        List<SaRetouerInfoVo> saRetouerInfoVos = orderService.saRouterInfozhao(searchInfo);
        return saRetouerInfoVos;
    }

    @RequestMapping("/zhao_weo")
    @ResponseBody
    public List<SaExchangInfoVo> saExchangInfozhao(String searchInfo){
        List<SaExchangInfoVo> saExchangInfoVos = orderService.saExchangInfozhao(searchInfo);
        return saExchangInfoVos;
    }

    /**
     * 合同信息栏
     * @param orderId
     * @return
     */
    @RequestMapping("/contract_orderInfo")
    @ResponseBody
    public List<OrderInfo> contractOrderInfo(Integer orderId){
        List<OrderInfo> orderInfos = orderService.contractOrderInfo(orderId);
        return orderInfos;
    }


   /* *//**
     * 合同查询
     * int empid
     * @return
     *//*
    @RequestMapping("/cc")
    @ResponseBody
    public List<Contract> contractInfo(Integer page,Integer size,Integer type,String name){
        //员工编号
        Integer empid=1001;
        Integer deptId=1;
        System.err.println("合同合同合同合同page "+page);
        System.err.println("合同合同合同合同size "+size);
        System.err.println("合同合同合同合同value "+type);
        System.err.println("合同合同合同合同contractName "+name);

        //Page<Object> pageinfo= PageHelper.startPage(page,size);
        List<Contract> contracts = contractService.contractsInfo(empid,type,name);
        Map<String,Object> map = new HashMap<>();
        //map.put("total",pageinfo.getTotal());
        //map.put("rows",pageinfo.getResult());
        return contracts;
    }

*/
    /**
     * 订单查询
     */
    @RequestMapping("/order_info")
    @ResponseBody
    public Map<String,Object> orderInfoList(Integer page,Integer size,Integer type,String name,Integer userId){
        Page<Object> pageinfo= PageHelper.startPage(page,size);
        List<OrderInfo> orderInfos = orderService.orderInfos(userId,type,name);
        Map<String,Object> map = new HashMap<>();
        map.put("rows",pageinfo.getResult());
        map.put("total",pageinfo.getTotal());
        return map;
    }


    @RequestMapping("/orderdetails_info")
    @ResponseBody
    public OrderInfo orderDetails(Integer orderId,Integer userId){
        OrderInfo orderInfo = orderService.orderInfo(userId,orderId);
        return orderInfo;
    }

    /**
     * 收货人地址查询
     * @param cusId
     * @return
     */
    @RequestMapping("/rece_info")
    @ResponseBody
    public List<Receivingcontact> receInfoList(Integer cusId){
        List<Receivingcontact> receivingcontacts = orderService.receInfos(cusId);
        return receivingcontacts;
    }

    /**
     * 新增订单
     * @param request
     */
    @RequestMapping("/save_order")
    @ResponseBody
    public void orderSave( HttpServletRequest request){
        OrderInfo order = JSON.parseObject(request.getParameter("order"),OrderInfo.class);
        order.setOrderAppstate(0);
        orderService.saveOrder(order);
        List<Orderdetails> list = new ArrayList<>();
        System.err.println("xxxx"+order.getOrderdetailsByOrderId());
        // order.getOrderdetailsByOrderId().
        //orderService.saveOrderDetail(order.getOrderdetailsByOrderId());

    }

    /**
     * 订单新增
     * @param orderInfo
     */
    @MyLog(type = "订单",value = "新增了订单")
    @RequestMapping("/save_ordertt")
    @ResponseBody
    public void saveOrderce(@RequestBody OrderInfo orderInfo){
        orderInfo.setOrderState(1);
        orderInfo.setOrderAppstate(0);
        orderService.saveOrder(orderInfo);
        System.err.println("产品名称产品名称产品名称"+orderInfo.getOrderdetailsByOrderId().get(0).getPrName());
        for (int i=0;i<orderInfo.getOrderdetailsByOrderId().size();i++){
            orderInfo.getOrderdetailsByOrderId().get(i).setOrderInfo(orderInfo);
            orderInfo.getOrderdetailsByOrderId().get(i).setReturnState(0);
            orderInfo.getOrderdetailsByOrderId().get(i).setExchangeState(0);
            System.err.println(orderInfo.getOrderdetailsByOrderId().get(i).getOrderInfo());
        }
        orderService.saveOrderDetail(orderInfo.getOrderdetailsByOrderId());
    }

    @RequestMapping("/order_products")
    @ResponseBody
    public List<Products> productsList(Integer businesId){
        List<Products> products = orderService.productsList(businesId);
        return  products;
    }


    @RequestMapping("/details_products")
    @ResponseBody
    public List<Orderdetails> orderdetails(Integer orderId){
        List<Orderdetails> list = orderService.orderDetailDaos(orderId);
        return list;
    }

    @RequestMapping("/invoiceInfo_list")
    @ResponseBody
    public   List<OrderInfo>invoiceInfo(String name,String timeValue){
        String[] date=new String[2];
        String startTime="";
        String endTime="";
        if(timeValue!=null && timeValue!=""){
            date=timeValue.split(",");
            startTime=date[0];
            endTime=date[1];
        }
        System.err.println("代处理的时间"+startTime+endTime);
        System.err.println("代处理的查询"+name);
        List<OrderInfo> orderInfos = orderService.orderInfos(name,startTime,endTime);
        return orderInfos;
    }


    /**
     * 发货单》》订单交付管理界面
     * @param orderId
     * @return
     */
    @RequestMapping("/deliverydetails_list")
    @ResponseBody
    public List<Deliverydetails> deliverydetailsList(Integer orderId){
        List<Deliverydetails> deliverydetailsList = orderService.deliverydetailsList(orderId);
        return deliverydetailsList;
    }

    /**
     * 指定订单》》指定的交付计划
     * @param orderId
     * @param deliveryId
     * @return
     */
    @RequestMapping("/deliveryProduct_list")
    @ResponseBody
    public List<DeliveryplanVo> deliveryplanVoList(Integer orderId,Integer deliveryId){
        List<DeliveryplanVo> deliveryplanVos = orderService.deliveryplanVoList(orderId, deliveryId);
        return deliveryplanVos;
    }



    @RequestMapping("/receiving_list")
    @ResponseBody
    public Receivingcontact receivingcontact(Integer ctId){
        Receivingcontact receivingcontact = orderService.receivList(ctId);
        return receivingcontact;
    }


    /*customeId:'',
    fhuodan:'',
    fhuoDate:'',
    date1:'',
    kauidgs:'',
    kauidhao:'',
    shouhdz:'',
    fzr:'1001',*/

    @RequestMapping("/fanhuoOk_save")
    @ResponseBody
    public void fanhuoOk(Integer customeId,String fhuodan, BigDecimal amount,
                         String kauidgs,String kauidhao,Integer shouhdz,Integer fzr,Integer orderId,Integer deliveryId){

       /* System.out.println("customeId"+customeId);//客户
        System.out.println("fhuodan"+fhuodan);//发货单
        System.out.println("amount"+amount);//金额
        System.out.println("kauidgs"+kauidgs);//快递公司
        System.out.println("kauidhao"+kauidhao);//快递单
        System.out.println("shouhdz"+shouhdz);//收货人id
        System.out.println("fzr"+fzr);//负责人
        System.out.println("orderId"+orderId);//订单id
        System.out.println("deliveryId"+deliveryId);//交付id*/
        orderService.saveFhuodan( customeId, fhuodan,  amount,
                 kauidgs, kauidhao, shouhdz, fzr, orderId, deliveryId);

    }

    @RequestMapping("/zfhuodan_list")
    @ResponseBody
    public List<ZfhuodanEntity> zfhuodanList(){
        List<ZfhuodanEntity> zfhuodanEntities = orderService.zfhuodanList();
        return zfhuodanEntities;
    }


    @RequestMapping("/limit_number")
    @ResponseBody
    public List<OrderProductNumberVo> orderProductNumberVoList(Integer id){
        List<OrderProductNumberVo> orderProductNumberVos = orderService.productNumberVoList(id);
        return orderProductNumberVos;
    }


    @RequestMapping("/zfhuodan_record")
    @ResponseBody
    public List<ZfhuodanEntity> zfhuodanEntityList(Integer id){
        List<ZfhuodanEntity> zfhuodanEntities =orderService.zfhuodanEntityList(id);
        return zfhuodanEntities;

    }


    @RequestMapping("/zhidng_deliceryProduct")
    @ResponseBody
    public List<Orderdetails> orderdetails(Integer orderId,Integer deliveyId){
        List<Orderdetails> orderdetails = orderService.orderdetails(orderId, deliveyId);
        return orderdetails;
    }
}
